package com.iyue.user.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.iyue.SpringHelper;
import com.iyue.Util;
import com.iyue.framework.cache.IMemcachedCache;
import com.iyue.framework.util.GsonUtil;

/**
 * 《用户管理》实用类
 * 
 * @author 孙晓晔
 * @version $Id: UserUtil.java,v 1.1 2010/10/14 17:07:17 fresh Exp $
 */
public class UserUtil extends Util {

	/**
	 * 判断是否是电子邮件
	 * 
	 * @param num
	 * @return
	 */
	public boolean isEmail(String email) {
		if (StringUtils.isBlank(email)) {
			return false;
		}
	
		return email.matches(".+@.+\\.[a-z]+");
	}

	/**
	 * 更新用户资料
	 * 
	 * @param username
	 */
	protected void update_user_cache(String key, Object ret, Integer time) {
		IMemcachedCache cache = (IMemcachedCache) SpringHelper
				.getBean("UserIMemcachedCache");
		if (ret instanceof String) {
			cache.add(key, ret, time);
		} else {
			cache.add(key, GsonUtil.getGson().toJson(ret), time);
		}
	}

	protected void clean_user_cache(String key) {
		IMemcachedCache cache = (IMemcachedCache) SpringHelper
				.getBean("UserIMemcachedCache");

		cache.delete(key);
	}

	/**
	 * 改变时间格式
	 * 
	 * @param date
	 * @param form
	 * @return
	 */
	public String convertDate(String date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date d;
		try {
			if (date == null || "".equals(date)) {
				d = new Date();
			} else {
				d = sdf.parse(date);
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}

		if (date == null || date.equals("1900-01-01 00:00:00")) {
			d = new Date();
		}

		if (format == null || "".equals(format)) {
			format = "yyyyMM";
		}
		sdf = new SimpleDateFormat(format);

		return sdf.format(d);
	}

	/**
	 * 判断是否是手机号
	 * 
	 * @param num
	 * @return
	 */
	public boolean isMobile(String mobile) {
		if (mobile == null) {
			return false;
		}
	
		return mobile.matches("1\\d{10}");
	}
}
